﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace natureskitchen
{
    public partial class frmNewExpense : Form
    {
        public frmNewExpense()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public bool checkBlanks()
        {
            if (txtDescription.Text == string.Empty)
            {
                MessageBox.Show("Please enter in a description for the new expense");
                return false;
            }
            if (txtCost.Text == string.Empty)
            {
                MessageBox.Show("Please enter in a cost for the new expense");
                return false;
            }
            return true;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (checkBlanks())
            {
                try
                {
                    mysql.connectDB();
                    string addquery = "ALTER TABLE Expenses ADD " + txtDescription.Text + " VARCHAR(60)";
                    //MySql.Data.MySqlClient.MySqlCommand command = new MySql.Data.MySqlClient.MySqlCommand(addquery, mysql.connectDB());
                    //command.ExecuteNonQuery();
                    mysql.disconnectDB();
                }
                catch (MySql.Data.MySqlClient.MySqlException mysqlEX)
                {
                    MessageBox.Show("An error occurred while connecting to the database: " + mysqlEX.Message);
                }
                catch (InvalidOperationException ioe)
                {
                    MessageBox.Show("An error has occurred: " + ioe.Message);
                }

                frmExpensesSummary es = Owner as frmExpensesSummary;

                Label lblnewexpense = new Label();
                lblnewexpense.Name = "lbl" + txtDescription.Text;
                lblnewexpense.Text = txtDescription.Text;
                lblnewexpense.Location = new Point(21, es.Height - 70);

                Label lblds = new Label();
                lblds.Name = "lbldsDefault";
                lblds.Text = "$";
                lblds.Location = new Point(157, es.Height - 70);

                TextBox txtnewtextbox = new TextBox();
                txtnewtextbox.Name = "txt" + txtDescription.Text;
                txtnewtextbox.Text = txtCost.Text;
                txtnewtextbox.Location = new Point(176, es.Height - 70);
                txtnewtextbox.ReadOnly = true;

                Button btnnewbutton = new Button();
                btnnewbutton.Name = "btnEdit" + txtDescription.Text;
                btnnewbutton.Text = "Edit";
                btnnewbutton.Size = new Size(50, 23);
                btnnewbutton.Location = new Point(296, es.Height - 70);

                es.btnCancel.Location = new Point(271, es.Height - 27);
                es.btnNewExpense.Location = new Point(12, es.Height - 27);
                es.btnUpdate.Location = new Point(154, es.Height - 27);

                es.Height = es.Height + 44;
                es.Controls.Add(lblnewexpense);
                es.Controls.Add(txtnewtextbox);
                es.Controls.Add(btnnewbutton);
                es.Controls.Add(lblds);
                es.Show();
                this.Close();
            }
        }
    }
}
